const connection = require('../app/database');
const { formatTimestamp, getMonthDate } = require('../utils/common');

class OverviewService {
  todayStartTime = ''; // 今天的起始日期
  todayEndTime = ''; // 今天的终止日期
  monthStartTime = ''; // 本月的起始日期
  monthEndTime = '';  // 本月的终止日期
  constructor() {
    const todayStartTimestamp = new Date(new Date().toLocaleDateString()).getTime();
    const todayEndTimestamp = new Date(new Date().toLocaleDateString()).getTime() + 24 * 60 * 60 * 1000 - 1;
    const todayStartTime = formatTimestamp(todayStartTimestamp);
    const todayEndTime = formatTimestamp(todayEndTimestamp);
    this.todayStartTime = todayStartTime;
    this.todayEndTime = todayEndTime;
    this.getMonth();
  }

  getMonth() {
    const result = getMonthDate();
    this.monthStartTime = result[0];
    this.monthEndTime = result[1];
  }

  // 获取今日订单完成订单数
  // 今日创建的订单，且今日完成的订单数
  async getFinishOrderToday() {
    try {
      const statement = `
        SELECT COUNT(*) finish 
        FROM orderList 
        WHERE status >=3 
          AND createTime > ?
          AND finishTime BETWEEN ? AND ?;
      `;
      const [result] = await connection.execute(statement, [this.todayStartTime, this.todayStartTime, this.todayEndTime]);
      console.log('[overview.service_getFinishOrderToday]{success}', result);
      return result[0]['finish'];
    } catch(e) {
      console.log('[overview.service_getFinishOrderToday]{fail}', e);
    }
  }

  // 获取今日创建的订单的总数
  async getTotalOrderToday() {
    try {
      const statement = `
        SELECT COUNT(*) total 
        FROM orderList 
        WHERE createTime > ?;
      `;
      const [result] = await connection.execute(statement, [this.todayStartTime]);
      console.log('[overview.service_getTotalOrderToday]{success}', result);
      return result[0]['total'];
    } catch(e) {
      console.log('[overview.service_getTotalOrderToday]{fail}', e);
    }
  }

  // 获取本月完成订单数
  async getFinishOrderMonth() {
    try {
      const statement = `
        SELECT COUNT(*) finish 
        FROM orderList 
        WHERE status >=3 
          AND createTime BETWEEN ? AND ?
          AND finishTime BETWEEN ? AND ?;
      `;
      const [result] = await connection.execute(statement, [this.monthStartTime, this.monthEndTime, this.monthStartTime, this.monthEndTime]);
      console.log('[overview.service_getFinishOrderMonth]{success}', result);
      return result[0]['finish'];
    } catch(e) {
      console.log('[overview.service_getFinishOrderMonth]{fail}', e);
    }
  }

  // 获取本月创建的订单数
  async getTotalOrderMonth() {
    try {
      const statement = `
        SELECT COUNT(*) total 
        FROM orderList 
        WHERE createTime BETWEEN ? AND ?;
      `;
      const [result] = await connection.execute(statement, [this.monthStartTime, this.monthEndTime]);
      console.log('[overview.service_getFinishOrderMonth]{success}', result);
      return result[0]['total'];
    } catch(e) {
      console.log('[overview.service_getTotalOrderMonth]{fail}', e);
    }
  }

  // 获取本月新增用户
  async getNewUserMonth() {
    try {
      const statement = `
        SELECT COUNT(*) new FROM user WHERE createAt BETWEEN ? AND ?;
      `;
      const [result] = await connection.execute(statement, [this.monthStartTime, this.monthEndTime]);
      console.log('[overview.service_getNewUserMonth]{success}', result);
      return result[0]['new'];
    } catch(e) {
      console.log('[overview.service_getNewUserMonth]{fail}', e);
    }
  }

  // 获取今日新增用户
  async getNewUserToday() {
    try {
      const statement = `
        SELECT COUNT(*) new FROM user WHERE createAt BETWEEN ? AND ?;
      `;
      const [result] = await connection.execute(statement, [this.todayStartTime, this.todayEndTime]);
      console.log('[overview.service_getNewUserToday]{success}', result);
      return result[0]['new'];
    } catch(e) {
      console.log('[overview.service_getNewUserToday]{fail}', e);
    }
  }

  // 获取本月新增员工
  async getNewWorkerMonth() {
    try {
      const statement = `
        SELECT COUNT(*) new FROM worker WHERE createAt BETWEEN ? AND ?;
      `;
      const [result] = await connection.execute(statement, [this.monthStartTime, this.monthEndTime]);
      console.log('[overview.service_getNewWorkerMonth]{success}', result);
      return result[0]['new'];
    } catch(e) {
      console.log('[overview.service_getNewWorkerMonth]{fail}', e);
    }
  }

  // 获取今日新增员工
  async getNewWorkerToday() {
    try {
      const statement = `
        SELECT COUNT(*) new FROM worker WHERE createAt BETWEEN ? AND ?;
      `;
      const [result] = await connection.execute(statement, [this.todayStartTime, this.todayEndTime]);
      console.log('[overview.service_getNewUserToday]{success}', result);
      return result[0]['new'];
    } catch(e) {
      console.log('[overview.service_getNewUserToday]{fail}', e);
    }
  }

  // 获取本月好评订单数(评分 > 4)
  async getGoodEvaMonth() {
    try {
      const statement = `
        SELECT COUNT(*) good 
        FROM orderList 
        WHERE status >= 3
          AND evaluation > 4
          AND createTime BETWEEN ? AND ?;
      `;
      const [result] = await connection.execute(statement, [this.monthStartTime, this.monthEndTime]);
      console.log('[overview.service_getGoodEvaMonth]{success}', result);
      return result[0]['good'];
    } catch(e) {
      console.log('[overview.service_getGoodEvaMonth]{fail}', e);
    }
  }

  // 获取今日好评订单数(评分 > 4)
  async getGoodEvaToday() {
    try {
      const statement = `
        SELECT COUNT(*) good 
        FROM orderList 
        WHERE status >= 3
          AND evaluation > 4
          AND createTime BETWEEN ? AND ?;
      `;
      const [result] = await connection.execute(statement, [this.todayStartTime, this.todayEndTime]);
      console.log('[overview.service_getGoodEvaToday]{success}', result);
      return result[0]['good'];
    } catch(e) {
      console.log('[overview.service_getGoodEvaToday]{fail}', e);
    }
  }

  // 获取本月或今日差评
  async getBadEva(isMonth) {
    try {
      const statement = `
        SELECT COUNT(*) bad 
        FROM orderList 
        WHERE status >= 3
          AND evaluation BETWEEN 0 AND 3
          AND createTime BETWEEN ? AND ?;
      `;
      let array = [];
      array = isMonth === 'true'
        ? [this.monthStartTime, this.monthEndTime]
        : [this.todayStartTime, this.todayEndTime];
      const [result] = await connection.execute(statement, array);
      console.log('[overview.service_getBadEva]{success}', `isMonth: ${isMonth}`, result);
      return result[0]['bad'];
    } catch(e) {
      console.log('[overview.service_getBadEva]{fail}', `isMonth: ${isMonth}`, e);
    }
  }

  // 根据维修类型获取对应的订单数量
  async getCountByWorkerType(timeArr) {
    try {
      const statement = `
        SELECT w.work, COUNT(*) count
        FROM  workType w
        LEFT JOIN orderList o ON w.id = o.work_type 
        WHERE o.createTime BETWEEN ? AND ?
        GROUP BY w.id;
      `;
      const [result] = await connection.execute(statement, timeArr);
      return result;
    } catch (e) {
      console.log('[overview.service_getCountByWorkerType]{fail}', e);
    }
  }
}

module.exports = new OverviewService()